<?php
/**
 * @Notes:【】
 * @Date: 2022-06-03 0:41
 */

namespace Yj\Generator\Core\Dao\Processor;

use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Str;
use Yj\Generator\Core\Common\Template\ClassNameTemplate;
use Yj\Generator\Core\Common\Template\DocBlockTemplate;
use Yj\Generator\Core\Common\Template\UsesTemplate;
use Yj\Generator\Core\Dao\Config;
use Yj\Generator\Core\Dao\Template\BaseTemplate;
use Yj\Generator\Core\Helper\Helper;

class ClassNameProcessor
{
    /**
     * @Inject()
     * @var Helper
     */
    protected $helper;

    /**
     * @inheritdoc
     */
    public function process(BaseTemplate $baseTemplate, Config $config)
    {
        $className = $config->get('class_name');
        $className = Str::studly($className);
        $baseClassName = $config->get('base_class_name');
        $baseTemplate->addUses(new UsesTemplate($baseClassName));
        $baseTemplate->addUses(new UsesTemplate($this->getModel($config)));
        $baseTemplate->setDocBlock(new DocBlockTemplate(["@var {$className}", "@method \$this params(\$params)"]));
        $baseTemplate->setClassName(new ClassNameTemplate($className . 'Dao', $this->helper->getShortClassName($baseClassName)));
    }

    public function getModel(Config $config)
    {
        $namespace = '\\App\\Model';
        if ($config->has('dirname') && $config->get('dirname')) {
            $namespace = $namespace . '\\' . Str::ucfirst($config->get('dirname'));
        }
        return $namespace . '\\' . Str::studly($config->get('class_name'));
    }

}